package com.bzgwl.cloud_gateway.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;

/**
 * @author Professor_Kong
 * @version 1.0
 * @date 2020/3/9 13:59
 */
@Configuration
public class ResourceServerConfig {


    private static final String RESOURCE_ID = "res_getaway_id";

    //oauth资源服务配置
    @Configuration
    @EnableResourceServer
    public class OauthServerConfig extends ResourceServerConfigurerAdapter {



        @Autowired
        private TokenStore tokenStore;

        @Override
        public void configure(ResourceServerSecurityConfigurer resources){
            //认证这个对所有服务都不拦截，所以不需要配置资源ID 表示所有请求都可访问
            resources.tokenStore(tokenStore)
                    .resourceId(RESOURCE_ID)
                    .stateless(true);
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.authorizeRequests()
                    .antMatchers("/oauth2/**").permitAll()
                    .and().headers().frameOptions().disable() //解决'X-Frame-Options' to 'deny'.
                    .and().csrf().disable();
        }
    }


    //order资源
    @Configuration
    @EnableResourceServer
    public class OrderServerConfig extends ResourceServerConfigurerAdapter {


        @Autowired
        private TokenStore tokenStore;

        @Override
        public void configure(ResourceServerSecurityConfigurer resources){
            resources.tokenStore(tokenStore).resourceId(RESOURCE_ID)
                    .stateless(true);
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                    .authorizeRequests()
                    .antMatchers("/ord/**").access("#oauth2.hasScope('READ')")
                    .and().headers().frameOptions().disable() //解决'X-Frame-Options' to 'deny'.
                    .and().csrf().disable();
        }
    }



    //web资源
    @Configuration
    @EnableResourceServer
    public class WebServerConfig extends ResourceServerConfigurerAdapter {


        @Autowired
        private TokenStore tokenStore;

        @Override
        public void configure(ResourceServerSecurityConfigurer resources){
            resources.tokenStore(tokenStore).resourceId(RESOURCE_ID)
                    .stateless(true);
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                    .authorizeRequests()
                    .antMatchers("/web/**").permitAll()
                    .and().headers().frameOptions().disable() //解决'X-Frame-Options' to 'deny'.
                    .and().csrf().disable();
        }
    }


    //sys资源
    @Configuration
    @EnableResourceServer
    public class SysServerConfig extends ResourceServerConfigurerAdapter {


        @Autowired
        private TokenStore tokenStore;

        @Override
        public void configure(ResourceServerSecurityConfigurer resources){
            resources.tokenStore(tokenStore).resourceId(RESOURCE_ID)
                    .stateless(true);
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
                    .authorizeRequests()
                    .antMatchers("/sys/**").permitAll()
                    .and().headers().frameOptions().disable() //解决'X-Frame-Options' to 'deny'.
                    .and().csrf().disable();
        }
    }

    //配置其它的资源服务..
}
